package com.shao.intelligentsnake;

/**
 * 走尾巴策略
 *
 * @author shaohuailin
 * @date 2024/4/21
 */
public class TailActionStrategy implements ActionStrategy {

    // 起点位置
    private int startPosition;
    // 尾巴位置
    private int tailPosition;
    // 探测器
    private Probe probe;
    // 尾探测器
    private Probe tailProbe;

    public TailActionStrategy(int startPosition, int tailPosition, Probe probe) {
        this.startPosition = startPosition;
        this.tailPosition = tailPosition;
        this.probe = probe;
        this.tailProbe = probe;
    }

    @Override
    public int[] apply() {
        // 尾部附近的空闲位置
        int[] nearby = tailProbe.probe(tailPosition);
        for (int endPosition : nearby) {
            ShortActionStrategy strategy = new ShortActionStrategy(startPosition, endPosition, probe);
            int[] positions = strategy.apply();
            // 能达到该空闲位置，直接返回该路径，不探测其他空闲位置了。
            if (positions.length > 0) {
                return positions;
            }
        }
        return new int[0];
    }

    public void setTailProbe(Probe tailProbe) {
        this.tailProbe = tailProbe;
    }
}
